<template>
  <div>
    <h1>用户列表</h1>
    <!-- 条件查询 -->
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input v-model="searchForm.keyWord" clearable />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="searchForm.beginTime"
            type="date"
            value-format="YYYY-MM-DD HH:mm:ss"
            clearable
          />
          -
          <el-date-picker
            v-model="searchForm.endTime"
            type="date"
            value-format="YYYY-MM-DD HH:mm:ss"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 列表数据 -->
    <div>
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="userId" label="用户ID" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="userPass" label="密码" />
        <el-table-column prop="nickName" label="真实姓名" />
        <el-table-column prop="userMobile" label="手机号码" />
        <el-table-column prop="userEmail" label="邮箱" />
        <el-table-column prop="userAvatar" label="头像">
          <template #default="scope">
            <el-image
              v-if="scope.row.userAvatar != null && scope.row.userAvatar != ''"
              style="width: 50px; height: 50px"
              :src="scope.row.userAvatar"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
      </el-table>
      <!-- 分页 -->
      <div>
        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="searchForm.pageNum"
            v-model:page-size="searchForm.pageSize"
            :page-sizes="[2, 3, 4, 5]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";

import { getUserPage } from "../api/api";

//多选
const multipleSelection = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log(multipleSelection.value);
};

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  keyWord: "",
  beginTime: "",
  endTime: "",
  pageNum: 1,
  pageSize: 2,
});
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};

//条件查询
const onQuery = () => {
  loadList();
};

const loadList = () => {
  getUserPage(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadList();
});
</script>

<!-- 分页样式 -->
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
<!-- 查询表单样式 -->
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
